package cn.edu.gzgs.controller;

import cn.edu.gzgs.service.CaptchaService;
import cn.edu.gzgs.vo.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "验证码接口", description = "生成和获取验证码")
@RestController
@RequestMapping("/api/captcha")
public class CaptchaController {

    private final CaptchaService captchaService;

    @Autowired
    public CaptchaController(CaptchaService captchaService) {
        this.captchaService = captchaService;
    }

    @Operation(summary = "获取图形验证码", description = "生成一个新的图形验证码，并返回其ID和Base64编码的图片。")
    @GetMapping
    public ResponseEntity<ApiResponse<CaptchaService.CaptchaResponse>> getCaptcha() {
        try {
            CaptchaService.CaptchaResponse captchaResponse = captchaService.generateCaptcha();
            return ResponseEntity.ok(new ApiResponse<>(0, "验证码获取成功", captchaResponse));
        } catch (RuntimeException e) {
            // 在CaptchaService中，如果Redis操作失败会抛出RuntimeException
            return ResponseEntity.status(500).body(new ApiResponse<>(500, e.getMessage(), null));
        }
    }
} 